package com.lq.demo.demo.mode.mycache;

import java.util.Set;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

/**
 * 测试缓存
 */
public class CacheTest {

    public static void main(String[] args) throws InterruptedException {
        //test

        int i = 0;
        final String key = "thread";
        CacheManagerImpl cacheManager = new CacheManagerImpl();
        CacheListener cacheListener = new CacheListener(cacheManager);
        ExecutorService executorService = Executors.newCachedThreadPool();
        for (int j = 0; j < 100; j++) {
            executorService.execute(new Runnable() {
                @Override
                public void run() {
                    if (!cacheManager.isContains(key)) {
                        cacheManager.putCache(key, new EntitiCache(1, 0, System.currentTimeMillis()));
                    } else {
                        synchronized (cacheManager) {
                            int v = (Integer) cacheManager.getCacheDataByKey(key) + 1;
                            cacheManager.putCache(key, new EntitiCache(v, 0, System.currentTimeMillis()));
                        }
                    }

                }
            });
        }
//        executorService.awaitTermination(1, TimeUnit.HOURS);

        System.out.println(cacheManager.getCacheDataByKey(key).toString());
        Thread.sleep(1000);
        executorService.shutdownNow();
        cacheListener.shutdown();
//            cacheManager.putCache("test1", "test1的data");
//            Thread.sleep(2000);
//            if (i == 2) cacheManager.putCache("test2", new EntitiCache(1, 5000, System.currentTimeMillis()));
//            cacheManager.putCache(1, 1);
//            CacheListener cacheListener = new CacheListener(cacheManager);
//
//
//            Object cacheDataByKey = cacheManager.getCacheDataByKey(1);
//            System.out.println(cacheDataByKey);
//            Object test1 = cacheManager.getCacheDataByKey("test1");
//            EntitiCache test2 = cacheManager.getCacheByKey("test2");
//            System.out.println(test2);
//            boolean timeOut2 = cacheManager.isTimeOut("test2");
//            boolean timeOut3 = cacheManager.isTimeOut("test1");
//            System.out.println(timeOut2);
//            System.out.println(timeOut3);
//
//            cacheManager.putCache("test2" + (++i), new EntitiCache(1, 10000, System.currentTimeMillis()));
//
//            Set allKeys = cacheManager.getAllKeys();
//            System.out.println(allKeys.size());

    }
}
